//
//  CAChangePwdVC.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/6/27.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "CAChangePwdVC.h"

@interface CAChangePwdVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *oldPwd;
@property (weak, nonatomic) IBOutlet UITextField *newsPwd;
@property (weak, nonatomic) IBOutlet UITextField *againNewsPwd;

@end

@implementation CAChangePwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.title = @"修改密码";
    
    self.oldPwd.delegate = self;
    self.newsPwd.delegate = self;
    self.againNewsPwd.delegate = self;
    
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 15, 47, 17)];
    [rightButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [rightButton setTitle:@"保存" forState:(UIControlStateNormal)];
    [rightButton addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    //    [leftButton setBackgroundImage:[UIImage imageNamed:@"navbtn.png"] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    // Do any additional setup after loading the view from its nib.
}

- (void)save{
    if ([self.oldPwd.text isEqualToString:@""]) {
        [ShowMessage showCenterViewMessage:@"请输入旧密码"];
        return;
    }
    if ([self.newsPwd.text isEqualToString:@""]) {
        [ShowMessage showCenterViewMessage:@"请输入新密码"];
        return;
    }
    if ([self.againNewsPwd.text isEqualToString:@""]) {
        [ShowMessage showCenterViewMessage:@"请再次输入新密码"];
        return;
    }
    
    if (![self.newsPwd.text isEqualToString:self.againNewsPwd.text]) {
        [ShowMessage showCenterViewMessage:@"新密码两次输入不同，请重新输入"];
        self.newsPwd.text = @"";
        self.againNewsPwd.text = @"";
        [self.newsPwd becomeFirstResponder];
        return;
    }
    
    [self commitChangPwdToService];
    
}

- (void)commitChangPwdToService{
    
    [self showIndicator:@"正在提交，请稍后..."];
    
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:[LoginSingleton sharedLoginSingleton].token,@"token",[LoginSingleton sharedLoginSingleton].loginModel.ids,@"id",self.oldPwd.text,@"old_password",self.againNewsPwd.text,@"new_password", nil];
    
    
    __weak __typeof(self) weakSelf = self;
    
    [HTTPManager post:GET_URLString(@"/Api/Member/editPassword") parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self hideIndicator];
        if ([responseObject[@"msg"] isEqualToString:@"成功"]) {
            
            [ShowMessage showCenterViewMessage:@"修改成功"];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [ShowMessage showCenterViewMessage:responseObject[@"msg"]];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hideIndicator];
        
    }];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField == self.oldPwd) {
        [self.newsPwd becomeFirstResponder];
        return YES;
    }
    
    if (textField == self.newsPwd) {
        [self.againNewsPwd becomeFirstResponder];
        return YES;
    }
    
    [self.view endEditing:YES];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
